package com.liukai.todo_android.presenter;

import com.liukai.todo_android.common.api.TodoAPI;
import com.liukai.todo_android.common.base.BasePresenter;
import com.liukai.todo_android.call_back.IFindTodoCallBack;
import com.liukai.todo_android.common.utils.Constants;
import com.liukai.todo_android.common.utils.RetrofitManager;
import com.liukai.todo_android.entity.ItemVo;
import com.liukai.todo_android.entity.vo.ItemQueryCondition;
import com.liukai.todo_android.entity.vo.QueryPage;
import com.liukai.todo_android.entity.Resp;

import java.net.HttpURLConnection;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * @author ：liukai
 * @date ：Created in 2021/8/11 11:12 上午
 * @description：查找todo的网络请求
 * @modified By：
 * @version: $
 */
public class FindSchedulePresenter extends BasePresenter<IFindTodoCallBack> {

    /**
     * 查询已完成的待办
     *
     * @param userId
     */
    public void findFinishedScheduleByUserId(Long userId) {

        ItemQueryCondition itemQueryCondition = new ItemQueryCondition();
        Map<String, Object> conditionMap = itemQueryCondition.setUserId(userId)
                .setFinished(Constants.SCHEDULE_FINISHED)
                .buildMap();
        QueryPage queryPage = new QueryPage();
        Map<String, Object> queryPageMap = queryPage.setCurrPage(1).setPageSize(100).buildMap();
        findItemByConditionAndQueryPage(conditionMap, queryPageMap);

    }

    /**
     * 搜索未完成的待办
     *
     * @param userId
     * @param searchKey
     */
    public void searchNotFinishedSchedule(Long userId, String searchKey) {
        ItemQueryCondition itemQueryCondition = new ItemQueryCondition();
        Map<String, Object> conditionMap = itemQueryCondition.setUserId(userId)
                .setFinished(Constants.SCHEDULE_NOT_FINISHED)
                .setSearchKey(searchKey)
                .buildMap();
        QueryPage queryPage = new QueryPage();
        Map<String, Object> queryPageMap = queryPage.setCurrPage(1).setPageSize(100).buildMap();
        findItemByConditionAndQueryPage(conditionMap, queryPageMap);
    }

    /**
     * 搜索未完成的待办
     *
     * @param userId
     * @param searchKey
     */
    public void searchFinishedSchedule(Long userId, String searchKey) {
        ItemQueryCondition itemQueryCondition = new ItemQueryCondition();
        Map<String, Object> conditionMap = itemQueryCondition.setUserId(userId)
                .setFinished(Constants.SCHEDULE_FINISHED)
                .setSearchKey(searchKey)
                .buildMap();
        QueryPage queryPage = new QueryPage();
        Map<String, Object> queryPageMap = queryPage.setCurrPage(1).setPageSize(100).buildMap();
        findItemByConditionAndQueryPage(conditionMap, queryPageMap);
    }

    /**
     * 查询未完成的待办
     *
     * @param userId
     */
    public void findNotFinishedScheduleByUserId(Long userId) {
        ItemQueryCondition itemQueryCondition = new ItemQueryCondition();
        Map<String, Object> conditionMap = itemQueryCondition.setUserId(userId)
                .setFinished(Constants.SCHEDULE_NOT_FINISHED)
                .buildMap();
        QueryPage queryPage = new QueryPage();
        Map<String, Object> queryPageMap = queryPage.setCurrPage(1).setPageSize(100).buildMap();
        findItemByConditionAndQueryPage(conditionMap, queryPageMap);
    }


    /**
     * 获取一个待办事项
     *
     * @param itemVo
     */
    public void getOneItem(ItemVo itemVo) {
        getItemById(itemVo.getId());
    }

    /**
     * 根据id获取待办
     *
     * @param id
     */
    private void getItemById(Long id) {
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        TodoAPI todoAPI = retrofit.create(TodoAPI.class);
        Call<Resp> idTask = todoAPI.getItemById(id);
        idTask.enqueue(new Callback<Resp>() {
            @Override
            public void onResponse(Call<Resp> call, Response<Resp> response) {
                if (response.code() == HttpURLConnection.HTTP_OK) {
                    requestSuccess(response);
                } else {
                    requestFailure(response);
                }
            }

            @Override
            public void onFailure(Call<Resp> call, Throwable t) {
                requestFailure(t);
            }
        });
    }

    /**
     * 条件和分页查询
     *
     * @param condition 查询条件
     * @param queryPage 分页
     */
    private void findItemByConditionAndQueryPage(Map<String, Object> condition, Map<String, Object> queryPage) {

        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        TodoAPI todoAPI = retrofit.create(TodoAPI.class);
        Call<Resp> searchTask = todoAPI.searchItems(condition, queryPage);
        searchTask.enqueue(new Callback<Resp>() {
            @Override
            public void onResponse(Call<Resp> call, Response<Resp> response) {
                if (response.code() == HttpURLConnection.HTTP_OK) {
                    requestSuccess(response);
                } else {
                    requestFailure(response);
                }
            }

            @Override
            public void onFailure(Call<Resp> call, Throwable t) {
                requestFailure(t);
            }
        });

    }

    /**
     * 请求成功
     *
     * @param response
     */
    private void requestSuccess(Response<Resp> response) {
        Resp body = response.body();
        int code = body.getCode();
        if (code == 200) {
            mCallBack.findScheduleSuccess(body);
            return;
        }
        mCallBack.findScheduleError(body);
    }

    /**
     * 请求失败
     *
     * @param response
     */
    private void requestFailure(Response<Resp> response) {
        mCallBack.findScheduleError(Resp.err("获取失败"));
    }

    /**
     * 请求失败
     *
     * @param t
     */
    private void requestFailure(Throwable t) {
        mCallBack.findScheduleError(Resp.err("网络未连接"));
    }

}
